package exia.java.core.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import exia.java.core.entities.Commande;

class HibernateServiceCommandeImpl implements IServiceCommande {

	private static final String REMOTE_SERVICE_NAME = "Hibernate Remote Commande Service";
	private static final double REMOTE_SERVICE_VERSION = 0.1;
	private Session WORKING_SESSION;
	
	/**
	 * 
	 * @param workingSession
	 */
	public HibernateServiceCommandeImpl(final Session workingSession){
		super();
		WORKING_SESSION = workingSession;
	}
	
	public HibernateServiceCommandeImpl(){
		super();
	}
	
	public String getRemoteServiceName(){
		return REMOTE_SERVICE_NAME;
	}
	
	public void setWorkingSession(final Session workingSession){
		this.WORKING_SESSION = workingSession;
	}
	
	/*
	 * 
	 */
	public Session getWorkingSession(){
		return this.WORKING_SESSION;
	}
	
	/**
	 * 
	 * @return
	 */
	public double getRemoteServiceVersion(){
		return REMOTE_SERVICE_VERSION;
	}

	public boolean addCommande(final Commande commande)throws ServiceCommandeException {
		boolean isAdded = false;
		Transaction tx = null;
		if((WORKING_SESSION != null)&&(commande != null)){
			try {
				tx = WORKING_SESSION.beginTransaction();
				WORKING_SESSION.save(commande);
				tx.commit();
				if(tx.wasCommitted()){
					isAdded = true;
				}
			} catch (HibernateException ex) {
				ex.printStackTrace();
				throw new ServiceCommandeException(ex);
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to add the Commande.\n\tcause: either the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+")\nor the Commande parameter of the addCommande method is null.\n");
		}
		return isAdded;
	}

	public int countCommandes() throws ServiceCommandeException {
		int nbCommandes = 0;
		if(this.WORKING_SESSION != null){
			List<Commande> tmp = this.getCommandes();
			if(tmp.size() > 0){
				nbCommandes = tmp.size();
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to count the number of Commandes.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return nbCommandes;
	}

	public Commande getCommandeById(final int id) throws ServiceCommandeException {
		Commande commande = null;
		List<Commande> tmp = null;
		if(this.WORKING_SESSION != null){
			if(id > 0){
				tmp = this.getCommandes();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Commande c : tmp){
						if(c.getId() == id){
							commande = c;
						}
					}
				}
			}else{
				
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to get the Commande with id.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return commande;
	}

	public List<Commande> getCommandes() throws ServiceCommandeException {
		List<Commande> commandes = null;
		Iterator it = null;
		Commande c = null;
		Query query = null;
		if(this.WORKING_SESSION != null){
			try {
				commandes = new ArrayList<Commande>();
				String SQL_QUERY ="from Commande commande";
					 query = this.WORKING_SESSION.createQuery(SQL_QUERY);
					 if(query != null){
						 for(it=query.iterate();it.hasNext();){
					     	c = (Commande) it.next();
					     	commandes.add(c);
					     }
					 }
			} catch (HibernateException ex) {
				ex.printStackTrace();
				commandes = null;
				throw new ServiceCommandeException(ex);
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to get the Commandes.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return commandes;
	}

	public List<Commande> getCommandesByDate(final Date date) throws ServiceCommandeException {
		List<Commande> commandes = null;
		if(this.WORKING_SESSION != null){
			if(date != null){
				List<Commande> tmp = this.getCommandes();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Commande c : tmp){
						if(c.getDate().equals(date)){
							if(commandes == null){
								commandes = new ArrayList<Commande>();
							}
							commandes.add(c);
						}
					}
				}
			}else{
				throw new ServiceCommandeException("ID VALUE ERROR: The id parameter of the getCommandesById must be greater than 0.");
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to get the Commandes By Datye.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return commandes;
	}

	public List<Commande> getCommandesByIdClient(final int idClient) throws ServiceCommandeException {
		List<Commande> commandes = null;
		List<Commande> tmp = null;
		if(this.WORKING_SESSION != null){
			if(idClient > 0){
				tmp = this.getCommandes();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Commande c : tmp){
						if(c.getClient().getId() == idClient){
							if(commandes == null){
								commandes = new ArrayList<Commande>();
							}
							commandes.add(c);
						}
					}
				}
			}else{
				throw new ServiceCommandeException("ID VALUE ERROR: The idClient parameter of the getCommandesByIdClient must be greater than 0.");
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to get the Commandes By Client id.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return commandes;
	}

	public boolean hasCommande(final Commande commande) throws ServiceCommandeException {
		boolean has = false;
		List<Commande> tmp = null;
		if(this.WORKING_SESSION != null){
			if(commande != null){
				tmp = this.getCommandes();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Commande c : tmp){
						if(c.equals(commande)){
							has = true;
							break;
						}
					}
				}
			}else{
				throw new ServiceCommandeException("NULL POINTER ERROR: the Commande parameter of the hasCommande method is null.");
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to test if the Commande exists.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return has;
	}

	public boolean hasCommandes() throws ServiceCommandeException {
		boolean has = false;
		if(this.countCommandes() > 0){
			has = true;
		}
		return has;
	}

	public boolean removeCommande(final Commande commande) throws ServiceCommandeException {
		boolean isRemoved = false;
		Transaction tx = null;
		if(this.WORKING_SESSION != null){
			if((commande != null)){
				try {
					tx = this.WORKING_SESSION.beginTransaction();
					this.WORKING_SESSION.delete(commande);
					tx.commit();
					if(tx.wasCommitted()){
						isRemoved = true;
					}
				} catch (HibernateException ex) {
					ex.printStackTrace();
					throw new ServiceCommandeException(ex);
				}
			}else{
				throw new ServiceCommandeException("\nNULL POINTER ERROR: Commande parameter of the removeCommande method is null.\n");
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to remove the desired Commande.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return isRemoved;
	}

	public boolean updateCommande(final Commande commande) throws ServiceCommandeException {
		boolean isUpdated = false;
		Transaction tx = null;
		if(this.WORKING_SESSION != null){
			if((commande != null)){
				try {
					tx = this.WORKING_SESSION.beginTransaction();
					this.WORKING_SESSION.update(commande);
					tx.commit();
					if(tx.wasCommitted()){
						isUpdated = true;
					}
				} catch (HibernateException ex) {
					ex.printStackTrace();
					throw new ServiceCommandeException(ex);
				}
			}else{
				throw new ServiceCommandeException("\nNULL POINTER ERROR: Commande parameter of the removeCommande method is null.\n");
			}
		}else{
			throw new ServiceCommandeException("\nNULL SESSION ERROR: Unable to update the desired Commande.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return isUpdated;
	}

}
